// 添加页面的操作
import MyPlugin from "../../../main";

export function 正则搜索文件(this_:MyPlugin){
    // 监听输入框的 keydown 事件
    this_.registerDomEvent(document, 'keydown', (evt: KeyboardEvent) => {
        const target = evt.target as HTMLInputElement;
        if (evt.key === 'Enter' && target.getAttribute('enterkeyhint') === 'search' && target.type === 'search') {
            let value = target.value;
            // 将所有空格替换为 .*?
            // value = value.split(/\s+/).join('.*?') + '|' + value.split(/\s+/).reverse().join('.*?');
            const key = 'sjzSearch';
            if (target.value.includes(" ")) {
                // 如果包含空格，存储当前值
                localStorage.setItem(key, target.value);
                const parts = target.value.split(" ");
                const regex = parts.map(part => `(?=.*${part})`).join("") + ".*";
                value = regex
                // 如果值最前面没有 '/', 则在最前面添加 '/' 
                if (!value.startsWith('/')) {
                    value = '/' + value;
                }
                target.value = value; // 更新输入框的值
                console.log('在按下回车键后，搜索框的值被修改为: ', value);
            } else if(target.value.includes("*")&&target.value.includes(".")){
                // 如果不包含空格，尝试从localStorage中恢复之前的值
                const storedValue = localStorage.getItem(key);
                if (storedValue !== null) {
                    target.value = storedValue;
                }
            }
        }
    });

    this_.registerEvent(this_.app.workspace.on('layout-change', () => {
        const searchInput = document.querySelector('.search-input');
        if (searchInput instanceof HTMLInputElement) {
            searchInput.addEventListener('input', (event) => {

            });
        }
    }));
}
